import * as Cesium from "cesium/Cesium";

import { LayerEventType } from "../../src/event/EventType";
import Envelope from "../../src/geometry/Envelope";
import RectanglePrimitive from "../../src/overlay/primitive/RectanglePrimitive";
import PrimitiveLayer from "../../src/layer/vector/PrimitiveLayer";
import { OverlayEventType } from "../../src/event/EventType";
import GroundPolylinePrimitive from "../../src/overlay/primitive/GroundPolyinePrimitive";
import PolylinePrimitive from "../../src/overlay/primitive/PolylinePrimitive";
import EllipsePrimitive from "../../src/overlay/primitive/EllipsePrimitive";
import EllipsoidPrimitive from "../../src/overlay/primitive/EllipsoidPrimitive";

import PrimitiveSource from "../../src/data/source/vector/PrimitiveSource";

export function add_layer_primitive(earth){

    let source = new PrimitiveSource("primitive", {});

    let layer = new PrimitiveLayer("primitive", {
        source : source
    });
    // layer.on(LayerEventType.ADD, evt=>{
    //     console.log(evt.obj.name);
    // });
    //定义图层的Overlay拾取事件
    layer.on(LayerEventType.PICK, evt=>{
        console.log(evt.obj.id);
    });
    earth.addLayer(layer);

    let primitive = undefined;
    //primitive = add_primitive_rect(layer);
    //primitive = add_primitive_polyline(layer);
    //primitive = add_primitive_groundline(layer);
    //primitive = add_primitive_ellipse(layer);
    primitive = add_primitive_ellipsoid(layer);


    //注册Overlay点击事件
    primitive.on(OverlayEventType.CLICK, evt=>{
        console.log("[OverlayEventType.CLICK]:" + evt.obj.id);
    });
    //entity = add_entity_polyline(layer);

    
    //earth.zoomTo(primitive);
    //earth.zoomTo(layer);
}

function add_primitive_polyline(layer){
    let coordinates = [
        116, 39,
        116, 40,
        117, 40,
    ];

    let primitive = new PolylinePrimitive(coordinates);
    layer.addOverlay(primitive);
    return primitive;
}

function add_primitive_groundline(layer){
    let coordinates = [
        116, 39,
        116, 40,
        117, 40,
    ];

    let primitive = new GroundPolylinePrimitive(coordinates);
    layer.addOverlay(primitive);
    return primitive;
}

function add_primitive_ellipse(layer){
    let ellipse = {
          position : {
              lng : 116,
              lat : 39,
              alt : 0
          },
          semiMinorAxis: 50000.0,
          semiMajorAxis: 100000.0,
    };

    let primitive = new EllipsePrimitive(ellipse);
    layer.addOverlay(primitive);
    return primitive;
}

function add_primitive_ellipsoid(layer){
    let ellipsoid = {
        position : {
            lng : 116,
            lat : 39,
            alt : 15000.0
        },
        axes: {
            x: 30000.0,
            y: 20000.0, 
            z: 15000.0,
        }
   };

    let primitive = new EllipsoidPrimitive(ellipsoid);
    layer.addOverlay(primitive);
    return primitive;
}

function add_primitive_rect(layer){
    //let envelope = new Envelope(116,39,117,40);
    //let envelope = new Envelope(0,0,10,10);
    let envelope = new Envelope(114,39,115,40);

    let primitive = new RectanglePrimitive(envelope);
    layer.addOverlay(primitive);

    return primitive;
}

// function add_entity_polyline(layer){
    
//     let coordiates = [
//         // lng, lat, alt
//         116.14458984774065,
//         40.30602839249214,
//         0,
//         116.1934975247693,
//         40.30185115488014,
//         0,
//         116.21759905683979,
//         40.299040985998055,
//         0,
//         116.22622724920136,
//         40.28964653405332,
//         0,
//         116.23729779430624,
//         40.27389302668485,
//         0,
//     ];

//     let options = {
//         clampToGround: true,
//         // 线宽
//         width: 10.0,
//         // 材质
//         material: Cesium.Color.RED,
//     };

//     let polyline = new Polyline(coordiates, options);
//     layer.addOverlay(polyline);    
 
//     return polyline;
// }